/*
 * Copyright (c) 2015 YCY Group Holding Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 *
 * $Id: Pagination.java  2015-3-6 上午10:59:38 jackLiang $
 */
package com.jkwzms.common.pagination;

import java.io.Serializable;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.collect.Lists;

/**
 * 针对easyui的dataGrid特点重新实现分页接口
 * 
 * @author JackLiang
 * @date 2015-3-6
 * @version 1.0.0
 */
public class Pagination<T> implements Page<T>, Serializable {

    private static final long serialVersionUID = 2327885882538312390L;

    private final List<T>     rows             = Lists.newArrayList();
    private final Pageable    pageable;
    private final long        total;

    /**
     * Constructor of {@code Pagination}.
     * 
     * @param rows the content of this page, must not be {@literal null}.
     * @param pageable the paging information, can be {@literal null}.
     * @param total the total amount of items available
     */
    public Pagination(List<T> rows, Pageable pageable, long total){
        if (null == rows) {
            throw new IllegalArgumentException("rows must not be null!");
        }
        this.rows.addAll(rows);
        this.pageable = pageable;
        this.total = total;
    }

    public Pagination(List<T> rows){
        this(rows, null, null == rows ? 0 : rows.size());
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#getNumber()
     */
    @Override
    @JSONField(serialize = false)
    public int getNumber() {
        return pageable == null ? 0 : pageable.getPageNumber();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#getSize()
     */
    @Override
    @JSONField(serialize = false)
    public int getSize() {
        return pageable == null ? 0 : pageable.getPageSize();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#getTotalPages()
     */
    @Override
    @JSONField(serialize = false)
    public int getTotalPages() {
        return getSize() == 0 ? 1 : (int) Math.ceil((double) total / (double) getSize());
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#getNumberOfElements()
     */
    @Override
    @JSONField(serialize = false)
    public int getNumberOfElements() {
        return rows.size();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#getTotalElements()
     */
    @Override
    @JSONField(serialize = false)
    public long getTotalElements() {
        return total;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#hasPreviousPage()
     */
    @Override
    @JSONField(serialize = false)
    public boolean hasPreviousPage() {
        return getNumber() > 0;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#isFirstPage()
     */
    @Override
    @JSONField(serialize = false)
    public boolean isFirstPage() {
        return !hasPreviousPage();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#hasNextPage()
     */
    @Override
    @JSONField(serialize = false)
    public boolean hasNextPage() {
        return getNumber() + 1 < getTotalPages();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#isLastPage()
     */
    @Override
    @JSONField(serialize = false)
    public boolean isLastPage() {
        return !hasNextPage();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#nextPageable()
     */
    @Override
    @JSONField(serialize = false)
    public Pageable nextPageable() {
        return hasNextPage() ? pageable.next() : null;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#previousPageable()
     */
    @Override
    @JSONField(serialize = false)
    public Pageable previousPageable() {
        if (hasPreviousPage()) {
            return pageable.previousOrFirst();
        }

        return null;
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#iterator()
     */
    @Override
    @JSONField(serialize = false)
    public Iterator<T> iterator() {
        return rows.iterator();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#getContent()
     */
    @Override
    @JSONField(serialize = false)
    public List<T> getContent() {
        return Collections.unmodifiableList(rows);
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#hasContent()
     */
    @Override
    @JSONField(serialize = false)
    public boolean hasContent() {
        return !rows.isEmpty();
    }

    /*
     * (non-Javadoc)
     * @see org.springframework.data.domain.Page#getSort()
     */
    @Override
    @JSONField(serialize = false)
    public Sort getSort() {
        return pageable == null ? null : pageable.getSort();
    }

    public List<T> getRows() {
        return getContent();
    }

    public long getTotal() {
        return getTotalElements();
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals(Object obj) {

        if (this == obj) {
            return true;
        }

        if (!(obj instanceof PageImpl<?>)) {
            return false;
        }

        Pagination<?> that = (Pagination<?>) obj;

        boolean totalEqual = this.total == that.total;
        boolean contentEqual = this.rows.equals(that.rows);
        boolean pageableEqual = this.pageable == null ? that.pageable == null : this.pageable.equals(that.pageable);

        return totalEqual && contentEqual && pageableEqual;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode() {

        int result = 17;

        result = 31 * result + (int) (total ^ total >>> 32);
        result = 31 * result + (pageable == null ? 0 : pageable.hashCode());
        result = 31 * result + rows.hashCode();

        return result;
    }
}
